import java.util.Random;
import java.util.Scanner;

public class Lab_01 {
    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);

        System.out.println("===== 100以内加减法口算习题 =====");
        System.out.println("请依次计算以下50道题目：");
        System.out.println();

        int correctCount = 0;

        for (int i = 1; i <= 50; i++) {
            // 随机选择加法或减法，0代表加法，1代表减法
            int operation = random.nextInt(2);
            int num1, num2, result;

            if (operation == 0) { // 加法
                // 生成两个随机数，确保和不超过100
                num1 = random.nextInt(101); // 0-100
                num2 = random.nextInt(101 - num1); // 确保num1 + num2 ≤ 100
                result = num1 + num2;

                System.out.print("第" + i + "题: " + num1 + " + " + num2 + " = ");
            } else { // 减法
                // 生成两个随机数，确保被减数大于减数
                num1 = random.nextInt(101); // 0-100
                num2 = random.nextInt(num1 + 1); // 0-num1，确保num1 ≥ num2
                result = num1 - num2;

                System.out.print("第" + i + "题: " + num1 + " - " + num2 + " = ");
            }

            // 获取用户输入
            int userAnswer = scanner.nextInt();

            // 检查答案
            if (userAnswer == result) {
                System.out.println("正确!");
                correctCount++;
            } else {
                System.out.println("错误! 正确答案是: " + result);
            }

            System.out.println();
        }

        // 显示最终得分
        System.out.println("===== 测试结束 =====");
        System.out.println("您答对了 " + correctCount + " 道题，正确率为 " + (correctCount * 100 / 50) + "%");

        scanner.close();
    }
}